package sys.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import lombok.extern.slf4j.Slf4j;
import org.omg.CORBA.UserException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import sys.core.ResponseResult;
import sys.entity.User;
import sys.entity.vo.UserVo;
import sys.service.MenuService;
import sys.service.TbUserService;
import sys.core.annotation.LoginRequired;
import sys.core.annotation.RecordLog;
import sys.entity.PageData;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import sys.entity.TbUserEntity;
import sys.utils.SessionUtil;

import java.util.List;

@RestController
@Api(tags = "用户管理")
@RequestMapping("/tbUser")
@Slf4j
public class TbUserController {


    private final TbUserService service;

    @Autowired
    public TbUserController(TbUserService service) {
        this.service = service;
    }
    @Autowired
    private MenuService menuService;
    /**
     * 查询
     *
     * @return
     */
    @ApiOperation(value = "查询")
    @LoginRequired
    @RecordLog("查询用户信息")
    @PostMapping(value = "/select")
    public List<TbUserEntity> select(@RequestBody TbUserEntity entity) {
        return service.selectAll(entity);
    }

    /**
     * 模糊查询
     *
     * @return
     */
    @ApiOperation(value = "模糊查询")
    @LoginRequired
    @RecordLog("模糊查询用户信息")
    @PostMapping(value = "/likeSelect")
    public PageData<TbUserEntity> likeSelect(@RequestBody TbUserEntity entity) {
        return service.select(entity);

    }

    /**
     * 更新
     *
     * @return
     */
    @ApiOperation(value = "更新")
    @LoginRequired
    @RecordLog("更新用户信息")
    @PostMapping(value = "/update")
    public void update(@RequestBody TbUserEntity entity) {
        service.update(entity);
    }

    /**
     * 添加
     *
     * @return
     */
    @ApiOperation(value = "添加")
    @RecordLog(value = "添加用户")
    @PostMapping(value = "/add")
    public void add(@RequestBody TbUserEntity entity) {
        service.add(entity);
    }
    @ApiOperation(value = "注册")
    @RecordLog(value = "注册")
    @PostMapping(value = "/register")
    public ResponseResult register(@RequestBody UserVo entity) {
        if (!entity.getPassword().equals(entity.getSurePassword())){
          return ResponseResult.failed("确认密码有误");
        }
        TbUserEntity tbUserEntity = new TbUserEntity();
        BeanUtils.copyProperties(entity,tbUserEntity);
        service.add(tbUserEntity);
        return ResponseResult.success();
    }

    /**
     * 删除
     *
     * @return
     */
    @ApiOperation(value = "删除")
    @LoginRequired
    @RecordLog("删除用户")
    @PostMapping(value = "/delete")
    public void delete(@RequestBody TbUserEntity entity) {
        service.delete(entity);
    }

    /**
     * 导出excel
     *
     * @return
     */
    @ApiOperation(value = "导出excel")
    @LoginRequired
    @RecordLog("导出用户信息")
    @GetMapping("/exportExcel")
    public void exportExcel(TbUserEntity entity, HttpServletResponse response) {
        service.exportExcel(entity, response);
    }


    @ApiOperation(value = "获取当前用户")
    @LoginRequired
    @RecordLog("获取当前用户信息")
    @PostMapping("/getCurrentUser")
    public User getCurrentUser(HttpServletRequest request) {
        User user = SessionUtil.getUser(request);
        //根据用户id查询角色
        Integer roleId = menuService.selectRole(user.getId());
        //根据角色查询菜单
        Integer[] menuId = menuService.getMenuByUserId(roleId);
        user.setMenuId(menuId);
        return user;

    }

}
